<?php

class Sistema_LegendamedidasController extends App_Controller_Action
{

    private $_table     = 'LEGENDAMEDIDAS';
    private $_pk        = 'LGM_ID';
    private $_order_col = 'LGM_ID';
    private $_order     = 'ASC';
    private $_sta_col   = 'LGM_STA';
    private $obj        = '';
    private $form       = '';
    //public  $db = '';
     
    public function init() 
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->obj = new Sistema_Model_DbTable_Legendamedidas();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Legendamedidas();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $where = $this->_pk . ' > 0 ';
       
               
        $STA = $this->_getParam($this->_sta_col, 1);
        $where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        $dscfiltro = $this->_getParam('dscfiltro', '');
        
        if($dscfiltro > '') $where .= ' and LGM_NOM like "'. $dscfiltro . '%"';

        $this->view->dscfiltro = $dscfiltro;
        
        //$result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $query = 'SELECT SQL_CACHE * FROM LEGENDAMEDIDAS WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    public function editAction() {
       
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $data = $this->form->getValues();
                    
                    if($id == 0) $this->obj->insert($data);
                    else $this->obj->update($data, $this->_pk.' = ' . $id);
                                        
                    
                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.Errordb::descricaoError($ex->getCode()).'");</script>';
                echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            $data = array($this->_sta_col => 0);
            $ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
        }       
        //echo json_encode(array('ok' => $ok));
        $this->_helper->redirector('index');
    }

}